package cn.matrix42.potoo.format.markdown.element.text.code;

import java.util.ArrayList;
import java.util.List;

import cn.matrix42.potoo.format.markdown.element.MarkdownElement;
import cn.matrix42.potoo.lang.Preconditions;

public class CodeFences extends MarkdownElement {

    private String language = "";
    private List<String> codeLineList = new ArrayList<>();

    public CodeFences() {
    }

    public CodeFences(String language, List<String> codeLineList) {
        Preconditions.checkNotNull(language);
        Preconditions.checkNotNull(codeLineList);
        this.language = language;
        this.codeLineList = codeLineList;
    }

    public void setLanguage(String language) {
        this.language = language;
    }

    public String getLanguage() {
        return language;
    }

    public void addCodeLine(String codeLine) {
        Preconditions.checkNotNull(codeLine);
        codeLineList.add(codeLine);
    }

    public void addCodeLines(List<String> codeLines) {
        Preconditions.checkNotNull(codeLines);
        codeLineList.addAll(codeLines);
    }

    public List<String> getCode() {
        return codeLineList;
    }

    @Override
    public List<String> toMarkdownStringList() {
        List<String> list = new ArrayList<>();
        list.add("```" + language);
        list.addAll(codeLineList);
        list.add("```");
        list.add(context.getConfig().getEof());
        return list;
    }

    public static CodeFences of() {
        return new CodeFences();
    }

    public static CodeFences of(String language, List<String> codeLineList) {
        return new CodeFences(language, codeLineList);
    }
}
